const express=require("express");
const path=require("path");
var web=new express();//实例化一个服务器
web.get("/",function (request,response) {
    let main="./web/main.html";//相对路径
    main=path.resolve(main);//转化为绝对路径
    response.sendFile(main);
});
web.get("/data",function (request,response) {
    let data="./web/data.json";
    data=path.resolve(data);//转化为绝对路径
    response.sendFile(data);
});
web.get("/ajax.html",function (request,response) {
    let ajax="./web/ajax.html";
    ajax=path.resolve(ajax);//转化为绝对路径
    response.sendFile(ajax);
});
let ajaxData=[
    {
        id:1,
        name:"小红红"
    },
    {
        id:2,
        name:"小苹果"
    },
    {
        id:3,
        name:"小哪吒"
    }
]
web.get("/ajax",function (request,response) {
    let id=response.query["id"];
    let item=ajaxData.find(item=>{return item.id==id});
    response.json(item);
});
//vue
let students=[];//100个
for (let i=1;i<=100;i++){
    let student={num:i,name:"荷花"+i};
    students.push(student);
}
//vue.html 访问路径
web.get("/vue.html",function (request,response) {
    let vues="./web/vue.html";
    vues=path.resolve(vues);
    response.sendFile(vues);
});
web.get("/studentlist",function (request,response) {
    response.json(students)
})
//分页
web.get("/vuepage.html",function (req,res) {
    let page="./web/vuepage.html";
    page=path.resolve(page);
    res.sendFile(page);
});
web.get("/studentpage",function (req,res) {
    let pagecount=Number.parseInt(req.query["pagecount"]);
    let pagenum=Number.parseInt(req.query["numpage"]);
    let index=(pagenum-1)*pagecount;//开始索引
    let end=index+pagecount;
    let items=students.slice(index,end);
    res.json({items,sumcount:students.length});
})
web.use("/static",express.static(__dirname+"/web/static"));
//启动服务
var server=web.listen(8090,function () {
    var host=server.address().address;
    var post=server.address().port;
    console.log("应用实例，访问地址http:%s:%s",host,post);
});